package com.ai.sitting;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.ai.sitting.R;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import android.widget.TextView;

import org.json.JSONException;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private PosturePredictor predictor;
    private TextInputEditText sensorInputEditText;
    private TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图
        sensorInputEditText = findViewById(R.id.sensorInputEditText);
        MaterialButton predictButton = findViewById(R.id.predictButton);
        resultTextView = findViewById(R.id.resultTextView);

        // 初始化预测器
        try {
            predictor = new PosturePredictor(this);
        } catch (IOException e) {
            Toast.makeText(this, "模型加载失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
            finish();
            return;
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        // 设置按钮点击事件
        predictButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                predictPosture();
            }
        });
    }

    private void predictPosture() {
        String input = sensorInputEditText.getText().toString().trim();

        // 检查输入是否为空
        if (input.isEmpty()) {
            Toast.makeText(this, "请输入传感器数值", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            // 解析输入数值
            String[] values = input.split("\\s+");
            if (values.length != 6) {
                Toast.makeText(this, "请输入6个数值", Toast.LENGTH_SHORT).show();
                return;
            }

            // 转换为float数组
            float[] sensorValues = new float[6];
            for (int i = 0; i < 6; i++) {
                sensorValues[i] = Float.parseFloat(values[i]);
                if (sensorValues[i] < 0 || sensorValues[i] > 6.0) {
                    Toast.makeText(this, "数值必须在0到6.0之间", Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            // 进行预测
            boolean isCorrectPosture = predictor.predict(sensorValues);

            // 显示结果
            String result = isCorrectPosture ? "标准坐姿 (1)" : "不标准坐姿 (0)";
            resultTextView.setText(result);

        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的数值", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "预测出错: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (predictor != null) {
            predictor.close();
        }
    }
}